package com.pignest.hotel.ui.room;

import org.apache.commons.lang.StringUtils;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.Events;

import com.pignest.framework.util.Log;
import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.Room;
import com.pignest.hotel.beans.RoomType;
import com.pignest.hotel.service.RoomService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.UIUtils;

public class ManagerHistogram extends RoomHistogram {

	private static final long serialVersionUID = -8013830424907596416L;
	
	public ManagerHistogram() {
		super();
		setShowFlags(RoomView.SHOW_STATUS | RoomView.SHOW_FEATURE | RoomView.SHOW_SELF_SERVICE | RoomView.SHOW_ROOM_TYPE);
	}
	
	@Override
	public void onEvent(Event event) throws Exception {
		super.onEvent(event);
		if (Events.ON_DOUBLE_CLICK.equals(event.getName())) {
			Component c = (RoomView)event.getTarget();
			if (!(c instanceof RoomView))
				return;
			onEditRoom(((RoomView)c).getRoomId());
		}
	}
	
	public void onEditRoom(String roomId) {
		if (roomId == null) {
			roomId = getSelectedRoom();
			if (roomId == null) {
				UIUtils.alert("请先选择一个房间!");
				return;
			}
		}
		final RoomWindow win = (RoomWindow) Executions.createComponents(
				"/modules/room/roomWindow.zul", null, null);
		win.setRoomId(roomId);
		win.setClosable(true);
		try {
			win.doModal();
			if (win.isSuccess()) {
				RoomView rv = getView(roomId);
				if (rv != null)
					rv.updateRoom();
			}
		} catch (Exception e) {
			Log.e(e);
		}
	}
	
	@Override
	protected void addEventListener(RoomView rv) {
		rv.addEventListener(Events.ON_DOUBLE_CLICK, this);
	}
	
	@Deprecated
	public String addRoom(String startNumber, String endNumber, String typeId, String status) {
		if (StringUtils.isEmpty(startNumber) || StringUtils.isEmpty(typeId) || StringUtils.isEmpty(status)) {
			return "请填写必填项。";
		}
		RoomService roomService = Services.getRoomService();
		if (StringUtils.isEmpty(endNumber))
			endNumber = startNumber;
		int start = Utils.getInteger(startNumber, 0);
		int end = Utils.getInteger(endNumber, 0);
		if (start <= 100 || end < 100 || start > end || start / 100 != end / 100) {
			return "开始房间号与结束房间号填写非法！";
		}
		RoomType type = roomService.getRoomType(typeId);
//		RoomStatus2 status = roomService.getRoomStatus(statusId);
		if (type == null || status == null)
			return "房间状态或房间类型非法选择！";
		int c = 0;
		String failure = "";
		for (int i = start; i <= end; i++) {
			Room room = new Room();
			room.setName(String.valueOf(i));
			room.setUseStatus(status);
			room.setType(type);
			try {
				roomService.addRoom(room);
				c ++;
			} catch (Throwable e) {
				failure += i + ", ";
			}
		}
		if (c > 0) {
			refresh();
			if (StringUtils.isNotEmpty(failure)) {
				return "共新增了：" + c + " 个房间，但" + failure + "失败，可能是已经存在相同房间号。";
			}
		} else {
			return "新增失败，可能是已经存在相同房间号。";
		}
		return null;
	}
	
	public boolean deleteRoom(String room) {
		//检验
		try {
			RoomService roomService = Services.getRoomService();
			roomService.removeRoom(roomService.getRoomByName(room));
			return true;
		} catch (Exception e) {
		}
		return false;
	}
	
}
